#!/bin/bash
#
# RemoveSyncthingConflicts.sh
#
# Acts as you'd expect, displays syncthing conflict files, with the option to
# delete them all. Dependent on the script syncthing_findconflicts.py by
# sercaxto (Georg Luntz) as published here:-
# https://github.com/sercxanto/small_scripts/blob/master/syncthing_findconflicts.py
#
# TODO: Reimplement this in python as a syncthing_findconflicts.py fork


#
# FUNCTIONS
#


# Remove the temp file, if it exists
function DeleteTemp {
    if [ -e /tmp/SynConList ]
    then
	echo Cleaning up
	rm /tmp/SynConList
    fi
}


# Check and fix dependency
function DependencyFix {
    if [ $(which syncthing_findconflicts.py) ]
    then
	# syncthing_findconflicts.py found
	return
    else
	echo Missing dependency, attempting to fix
	# Download dependncy to /usr/bin
	curl https://raw.githubusercontent.com/sercxanto/small_scripts/master/syncthing_findconflicts.py > /usr/bin/syncthing_findconflicts.py
	
	# Check that file was created succesfully
	if [ -e /usr/bin/syncthing_findconflicts.py ]
	then
	    echo Downloaded, making executable
	    chmod +x /usr/bin/syncthing_findconflicts.py
	else
	    echo "Failed to repair. Either run this script with sudo or install https://github.com/sercxanto/small_scripts/blob/master/syncthing_findconflicts.py to somewhere in your path manually"
	    exit
	fi
    fi
}


#
# MAIN SCRIPT
#

# Check/fix dependencies
DependencyFix

# Run syncthing_findconflicts.py for user output
syncthing_findconflicts.py

# Blank out a temporary list file
cat /dev/null > /tmp/SynConList

# Output list of all syncthing conflict files
# 1. grep to find all the lines that start with some whitespace followed by a forward slash
# 2. sed to remove any of the trailing blank space, ready to delete
syncthing_findconflicts.py | grep '^[ ]*/' | sed s/\ *// > /tmp/SynConList

# Count found conflicts
line_count=$(cat /tmp/SynConList | wc -l )

# Wipe temp file and quit if nothing found
if [ $line_count = 0 ]
then
    DeleteTemp
    exit
fi

# Ask user if they want files removed
echo
echo "Do you want to remove all conflicts? (y/N)"
read userchoice

# Checl if user response starts with a "Y" (case insensitive)
if [ $(echo $userchoice | grep -i ^y | wc -l) -gt 0 ]
then
   echo Deleting...

   # Counter is zero, compared to ensure it is below the total number of lines in the list
   counter=0
   while [ $counter -lt $line_count ]; do
       # Immediatly incremented so it matches first line/last line
       let counter=$counter+1
       # Set current line - only output counter amount of lines, and take the bottom one
       # As counter increases, each line in turn is captured
       current=$(cat /tmp/SynConList | head -n$counter | tail -n1)
       echo Removing $current
       rm "$current"
   done
# If user responds "N"
elif [ $(echo $userchoice | grep -i ^n | wc -l) -gt 0 ];
then
     echo "Files will remain on disk"
else
    # User responds neither yes nor no
    echo "Please answer yes or no - exiting"
fi


# Wipe temp file
DeleteTemp
